package q1.project.modules.system.user.provider;

import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import q1.project.modules.system.user.dao.UserDao;
import q1.project.modules.system.user.entity.UserEntity;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author Q1 Team
 * @version 1.0.0
 * @since 2025-11-05
 */
@Slf4j
@Service
public class SysProviderService {

  private final UserDao userDao;

  public SysProviderService(UserDao userDao) {
    this.userDao = userDao;
  }

  /** 鐢ㄦ埛淇℃伅DTO */
  public static class UserInfo {
    private String userId;
    private String username;
    private String nickname;
    private String avatar;
    private String email;
    private String phone;

    public UserInfo() {}

    public UserInfo(String userId, String username, String nickname, String avatar) {
      this.userId = userId;
      this.username = username;
      this.nickname = nickname;
      this.avatar = avatar;
    }

    // Getters and Setters
    public String getUserId() {
      return userId;
    }

    public void setUserId(String userId) {
      this.userId = userId;
    }

    public String getUsername() {
      return username;
    }

    public void setUsername(String username) {
      this.username = username;
    }

    public String getNickname() {
      return nickname;
    }

    public void setNickname(String nickname) {
      this.nickname = nickname;
    }

    public String getAvatar() {
      return avatar;
    }

    public void setAvatar(String avatar) {
      this.avatar = avatar;
    }

    public String getEmail() {
      return email;
    }

    public void setEmail(String email) {
      this.email = email;
    }

    public String getPhone() {
      return phone;
    }

    public void setPhone(String phone) {
      this.phone = phone;
    }
  }

  /**
   * 鏍规嵁鐢ㄦ埛ID鑾峰彇鐢ㄦ埛淇℃伅
   *
   * @param userId 鐢ㄦ埛ID
   * @return 鐢ㄦ埛淇℃伅锛屽鏋滀笉瀛樺湪杩斿洖 null
   */
  public UserInfo getUserInfo(String userId) {
    Optional<UserEntity> userOpt = userDao.findById(userId);
    if (userOpt.isEmpty()) {
      return null;
    }

    UserEntity user = userOpt.get();
    UserInfo info = new UserInfo();
    info.setUserId(user.getId());
    info.setUsername(user.getUsername());
    info.setNickname(user.getNickname());
    info.setAvatar(user.getAvatar());
    info.setEmail(user.getEmail());
    info.setPhone(user.getPhone());
    return info;
  }

  /**
   * 鎵归噺鏍规嵁鐢ㄦ埛ID鑾峰彇鐢ㄦ埛淇℃伅锛堜紭鍖朜+1鏌ヨ锛?
   *
   * @param userIds 鐢ㄦ埛ID鍒楄〃
   * @return 鐢ㄦ埛ID鍒扮敤鎴蜂俊鎭殑鏄犲皠锛屼笉瀛樺湪鐨勭敤鎴蜂笉浼氬嚭鐜板湪Map涓?
   */
  public Map<String, UserInfo> getUsersByIds(List<String> userIds) {
    if (userIds == null || userIds.isEmpty()) {
      return Collections.emptyMap();
    }

    List<UserEntity> users = userDao.findAllById(userIds);
    return users.stream()
        .collect(
            Collectors.toMap(
                UserEntity::getId,
                user -> {
                  UserInfo info = new UserInfo();
                  info.setUserId(user.getId());
                  info.setUsername(user.getUsername());
                  info.setNickname(user.getNickname());
                  info.setAvatar(user.getAvatar());
                  info.setEmail(user.getEmail());
                  info.setPhone(user.getPhone());
                  return info;
                }));
  }

  /**
   * 鏍规嵁鐢ㄦ埛鍚嶈幏鍙栫敤鎴蜂俊鎭?
   *
   * @param username 鐢ㄦ埛鍚?
   * @return 鐢ㄦ埛淇℃伅锛屽鏋滀笉瀛樺湪杩斿洖 null
   */
  public UserInfo getUserInfoByUsername(String username) {
    Optional<UserEntity> userOpt = userDao.findByUsername(username);
    if (userOpt.isEmpty()) {
      return null;
    }

    UserEntity user = userOpt.get();
    UserInfo info = new UserInfo();
    info.setUserId(user.getId());
    info.setUsername(user.getUsername());
    info.setNickname(user.getNickname());
    info.setAvatar(user.getAvatar());
    info.setEmail(user.getEmail());
    info.setPhone(user.getPhone());
    return info;
  }

  /**
   * 妫€鏌ョ敤鎴锋槸鍚﹀瓨鍦?
   *
   * @param userId 鐢ㄦ埛ID
   * @return 濡傛灉瀛樺湪杩斿洖 true
   */
  public boolean existsUser(String userId) {
    return userDao.existsById(userId);
  }

  /**
   * 鑾峰彇绉熸埛涓嬬殑鐢ㄦ埛鍒楄〃锛堝垎椤碉級
   *
   * @param tenantId 绉熸埛ID
   * @param pageable 鍒嗛〉鍙傛暟
   * @return 鐢ㄦ埛鍒楄〃鍒嗛〉缁撴灉
   */
  public Page<UserInfo> getUsersByTenant(String tenantId, Pageable pageable) {
    Page<UserEntity> userPage = userDao.findByTenantId(tenantId, pageable);

    return userPage.map(
        user -> {
          UserInfo info = new UserInfo();
          info.setUserId(user.getId());
          info.setUsername(user.getUsername());
          info.setNickname(user.getNickname());
          info.setAvatar(user.getAvatar());
          info.setEmail(user.getEmail());
          info.setPhone(user.getPhone());
          return info;
        });
  }

  /**
   * 缁熻鎵€鏈夌敤鎴锋暟锛堣法绉熸埛锛?
   *
   * @return 鐢ㄦ埛鎬绘暟
   */
  public long countAllUsers() {
    return userDao.count();
  }

  /**
   * 缁熻浠婃棩鏂板鐢ㄦ埛鏁帮紙璺ㄧ鎴凤級
   *
   * @return 浠婃棩鏂板鐢ㄦ埛鏁?
   */
  public long countAllTodayNewUsers() {
    LocalDateTime todayStart =
        LocalDateTime.now().withHour(0).withMinute(0).withSecond(0).withNano(0);
    return userDao.countByCreateTimeAfter(todayStart);
  }
}
